<template>
	<view class="wrap">
		<view class="name">{{info.name}}</view>
		<!-- 注意，如果需要兼容微信小程序，最好通过setRules方法设置rules规则 -->
		<u--form labelPosition="left" :model="info" ref="uForm" class="form">
			<u-form-item label="持有:">
				<u--input v-model="info.have"><template slot="suffix">
						<u-button text="份" type="info" size="mini" :disabled="true"></u-button>
					</template></u--input>
			</u-form-item>
			<u-form-item label="剩余:">
				<u--input v-model="info.rest"><template slot="suffix">
						<u-button text="元" type="info" size="mini" :disabled="true"></u-button>
					</template></u--input>
			</u-form-item>
			<u-form-item label="高值:">
				<u--input v-model="info.high"><template slot="suffix">
						<u-button text="%" type="info" size="mini" :disabled="true"></u-button>
					</template></u--input>
			</u-form-item>
			<u-form-item label="低值:">
				<u--input v-model="info.low"><template slot="suffix">
						<u-button text="%" type="info" size="mini" :disabled="true"></u-button>
					</template></u--input>
			</u-form-item>
			<u-form-item label="当前处理:">
				<div class="">
					<u--input v-model="info.estimation">
						<template slot="prefix">
							<text class="">当前估值:</text>
						</template>
						<template slot="suffix">
							<u-button text="%" type="info" size="mini" :disabled="true"></u-button>
						</template>
					</u--input>
					<u--input v-model="info.current_difference">
						<template slot="prefix">
							<text class="">处理差值:</text>
						</template>
						<template slot="suffix">
							<u-button text="%" type="info" size="mini" :disabled="true"></u-button>
						</template>
					</u--input>
				</div>
			</u-form-item>
			<u-form-item label="累计差值:">
				<u--input v-model="info.total_difference">
					<template slot="suffix">
						<u-button text="%" type="info" size="mini" :disabled="true"></u-button>
					</template>
				</u--input>
			</u-form-item>
		</u--form>
		<view class="name">计算结果：{{info.estimation>0?'卖出':'买入'}}{{info.result}}</view>
		<view class="btn-wrap">
			<u-button class="btn" type="warning" text="实值确认" size="small" @click="submit"></u-button>
			<u-button class="btn" type="warning" text="新增" size="small" @click="submit"></u-button>
			<u-button class="btn" type="warning" text="试算" size="small" @click="submit"></u-button>
			<u-button class="btn" type="primary" text="估值提交" size="small" @click="submit"></u-button>
		</view>
		<view class="name">历史记录</view>
		<view class="">
			<view v-for="(item,index) in 20" :key="index">
				{{item}}
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showSex: false,
				info: {
					name: "",
					have: 17923.79,
					rest: 14210,
					high: 0,
					low: -9.81,
					estimation: -0.7,
					difference: 0,
					result: 0
				},
			};
		},
		onLoad(options) {
			for (const key in options) {
				this.info[key] = options[key]
			}
		},
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
		},
		methods: {
			submit() {
				const total = this.info.estimation > 0 ? this.info.have : this.info.rest
				const current_estimation = parseFloat(this.info.estimation) + parseFloat(this.info.current_difference)
				const final_estimation = Math.abs(current_estimation)
				const nx = this.handle_nx(this.info.high, this.info.low)

				const res = this.calculate_sale(total, final_estimation, nx)
				this.info.result = res
				console.log('xx', typeof parseFloat(this.info.estimation));
				console.log('res', res);
			},
			handle_nx(height, low) {
				const range = Math.abs(height - low)
				return `${Math.ceil(range / 5)}x`
			},
			calculate_sale(initialAmount, percent, nx) {
				const increasePercent = parseFloat(percent);
				const stepPercent = nx.slice(0, -1);

				// 核心计算逻辑
				const fullStages = Math.floor(increasePercent / stepPercent);
				const fraction = increasePercent % stepPercent;

				// 阶梯卖出后剩余量，每次剩0.75，就是0.75的n次方
				const remainingAfterFull = initialAmount * Math.pow(0.75, fullStages);
				// 完整阶梯卖出量计算
				const fullStageSold = initialAmount - remainingAfterFull;

				// 未满阶梯的线性卖出量
				const fractionalRatio = fraction / stepPercent;
				const fractionalSold = remainingAfterFull * 0.25 * fractionalRatio;

				return (fullStageSold + fractionalSold).toFixed(4);
			}
		},

	};
</script>

<style lang="scss">
	.wrap {
		padding: 10rpx 40rpx;

		.form {
			padding: 0 30rpx;
		}

		.name {
			margin: 15rpx 0;
			font-size: 36rpx;
			font-weight: 600;
			text-align: center;
		}

		.btn-wrap {
			display: flex;
		}

		.btn {
			width: 400rpx;
			margin: 30rpx 15rpx 20rpx;
		}
	}
</style>